#ifndef SYS_TIMER_H
#define	SYS_TIMER_H

#include "stdint.h"
#include "pincfg.h"

#ifdef   _TIMER_MODULE_
#define  TIM_EXT
#else
#define  TIM_EXT  extern
#endif      

#define SYSTICK_CYCLE             1000UL     //system tick timer interrupt cycle //us
#define COUNTER_10MS              ((uint8_t)((10UL*1000UL/SYSTICK_CYCLE)))         
#define COUNTER_20MS              ((uint8_t)((20UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_30MS              ((uint8_t)((30UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_50MS              ((uint8_t)((50UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_100MS             ((uint8_t)((100UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_200MS             ((uint8_t)((200UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_300MS             ((uint8_t)((250UL*1000UL/SYSTICK_CYCLE)))

#define COUNTER_125MS             ((uint16_t)((125UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_250MS             ((uint16_t)((250UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_500MS             ((uint16_t)((500UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_1S                ((uint16_t)((1000UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_2S                ((uint16_t)((2000UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_3S                ((uint16_t)((3000UL*1000UL/SYSTICK_CYCLE)))

TIM_EXT  uint16_t Counter_10ms;
TIM_EXT  uint16_t Counter_20ms;
TIM_EXT  uint16_t Counter_30ms;
TIM_EXT  uint16_t Counter_50ms;
TIM_EXT  uint16_t Counter_100ms;
TIM_EXT  uint16_t Counter_200ms;
TIM_EXT  uint16_t Counter_300ms;

TIM_EXT  uint16_t Counter_125ms;
TIM_EXT  uint16_t Counter_250ms;
TIM_EXT  uint16_t Counter_500ms;
TIM_EXT  uint16_t Counter_1s;
TIM_EXT  uint16_t Counter_2s;
TIM_EXT  uint16_t Counter_3s;

TIM_EXT  uint32_t AutoTimeCnt;                    //base on 100ms
TIM_EXT  uint32_t SysTimeCnt;                     //base on 1s

TIM_EXT  volatile uint32_t SysTickCnt;

TIM_EXT  volatile uint32_t bTimerPulseSt __attribute__((at(SYSTIMER_PULSE_BASE)));

#define bPulse_10ms      SramBitBand(SYSTIMER_PULSE_BASE,0)
#define bPulse_20ms      SramBitBand(SYSTIMER_PULSE_BASE,1)
#define bPulse_30ms      SramBitBand(SYSTIMER_PULSE_BASE,2)
#define bPulse_50ms      SramBitBand(SYSTIMER_PULSE_BASE,3)
#define bPulse_100ms     SramBitBand(SYSTIMER_PULSE_BASE,4)
#define bPulse_200ms     SramBitBand(SYSTIMER_PULSE_BASE,5)
#define bPulse_300ms     SramBitBand(SYSTIMER_PULSE_BASE,6)
#define bPulse_Tick      SramBitBand(SYSTIMER_PULSE_BASE,7)

#define bPulse_125ms     SramBitBand(SYSTIMER_PULSE_BASE,8)
#define bPulse_250ms     SramBitBand(SYSTIMER_PULSE_BASE,9)
#define bPulse_500ms     SramBitBand(SYSTIMER_PULSE_BASE,10)
#define bPulse_1s        SramBitBand(SYSTIMER_PULSE_BASE,11)
#define bPulse_2s        SramBitBand(SYSTIMER_PULSE_BASE,12)
#define bPulse_3s        SramBitBand(SYSTIMER_PULSE_BASE,13)

#define bWave_125ms      SramBitBand(SYSTIMER_PULSE_BASE,16)
#define bWave_250ms      SramBitBand(SYSTIMER_PULSE_BASE,17)
#define bWave_500ms      SramBitBand(SYSTIMER_PULSE_BASE,18)
#define bWave_1s         SramBitBand(SYSTIMER_PULSE_BASE,19)
#define bWave_2s         SramBitBand(SYSTIMER_PULSE_BASE,20)
#define bWave_3s         SramBitBand(SYSTIMER_PULSE_BASE,21)

#define PULSE_10MS_POS   SramBitBand(SYSTIMER_PULSE_BASE,0)
#define PULSE_20MS_POS   SramBitBand(SYSTIMER_PULSE_BASE,1)
#define PULSE_30MS_POS   SramBitBand(SYSTIMER_PULSE_BASE,2)
#define PULSE_50MS_POS   SramBitBand(SYSTIMER_PULSE_BASE,3)
#define PULSE_100MS_POS  SramBitBand(SYSTIMER_PULSE_BASE,4)
#define PULSE_200MS_POS  SramBitBand(SYSTIMER_PULSE_BASE,5)
#define PULSE_300MS_POS  SramBitBand(SYSTIMER_PULSE_BASE,6)
#define PULSE_TICK_POS   SramBitBand(SYSTIMER_PULSE_BASE,7)

#define PULSE_125MS_POS  SramBitBand(SYSTIMER_PULSE_BASE,8)
#define PULSE_250MS_POS  SramBitBand(SYSTIMER_PULSE_BASE,9)
#define PULSE_500MS_POS  SramBitBand(SYSTIMER_PULSE_BASE,10)
#define PULSE_1S_POS     SramBitBand(SYSTIMER_PULSE_BASE,11)
#define PULSE_2S_POS     SramBitBand(SYSTIMER_PULSE_BASE,12)
#define PULSE_3S_POS     SramBitBand(SYSTIMER_PULSE_BASE,13)

#define WAVE_125MS_POS   SramBitBand(SYSTIMER_PULSE_BASE,16)
#define WAVE_250MS_POS   SramBitBand(SYSTIMER_PULSE_BASE,17)
#define WAVE_500MS_POS   SramBitBand(SYSTIMER_PULSE_BASE,18)
#define WAVE_1S_POS      SramBitBand(SYSTIMER_PULSE_BASE,19)
#define WAVE_2S_POS      SramBitBand(SYSTIMER_PULSE_BASE,20)
#define WAVE_3S_POS      SramBitBand(SYSTIMER_PULSE_BASE,21)

#define SET_PULSE(x)            x=1
#define CLR_PULSE(x)            x=0
#define BS125MS_SET_PULSE(x)    x=1
#define BS125MS_CLR_PULSE(x)    x=0
#define INV_WAVE(x)             x=!x

void SysPulseCreat(void);
void SysPulseClear(void);
void SysTickInit(void);

#undef TIM_EXT

#endif	/* SYS_TIMER_H */

